package com.fingard.dsp.bank.directbank.cmbc01;

import java.math.BigDecimal;
import java.sql.Connection;
import java.util.Date;
import java.util.Map;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.bank.directbank.bankInterface.IElecBillBankTrans;
import com.fingard.dsp.bank.reqrespobj.elecbilltrans.*;
import com.fingard.dsp.bank.reqrespobj.elecbilltrans.detail.*;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

/**
 * Created by F on 2018/12/18.
 */
@Component("CMBC01ElecBillTrans")
public class CMBC01ElecBillTrans extends CMBC01Base implements IElecBillBankTrans {

    private static final String SELECTSEQ = "SELECT BANKREQSEQID FROM REQSEQIDMAPPING WHERE REQSEQID=? AND BANKCODE=?;";

    /**
     * 通用签收 b2e0109
     */
    @Override
    public RetAtsB003BillRespSubmit D_B003_BillResp_Submit(ReqB003BillRespSubmit query) throws Exception{
        RetAtsB003BillRespSubmit retAts = new RetAtsB003BillRespSubmit(query.reqHeader);
        RetAtsB003BillRespSubmitDetail detail=new RetAtsB003BillRespSubmitDetail();
        retAts.writeBeforeDetail();
        try{
            while (query.readRecord()) {
                if ("1".equals(query.detail.respType)) {
                    detail=b003BillRespSubmitOne(query,"AcceptSignUp");//1-承兑应答×
                }else if ("6".equals(query.detail.respType)){
                    detail=b003BillRespSubmitOne(query,"IssueSignUp");//6-提示付款应答×
                }else if ("2".equals(query.detail.respType)){
                    detail=b003BillRespSubmitOne(query,"IssuanceDestruction");//2-提示收票回复
                }else if ("3".equals(query.detail.respType)){
                    detail=b003BillRespSubmitOne(query,"EndorsementDraftSignUp");//3-背书回复ok
                }else {
                    detail.respCode="-1";
                    detail.respInfo="暂不支持此签收操作";
                }
            }
        }catch (Exception ex ){
            detail.respCode="-1";
            detail.respInfo="签收异常";
            ex.printStackTrace();
            WriteBankLogLn("签收异常:"+ex);
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 出票 b2e0101
     */
    @Override
    public RetAtsB101BillApplySubmit D_B101_BillResp_Submit(ReqB101BillApplySubmit query) throws Exception{
        RetAtsB101BillApplySubmit retAts=new RetAtsB101BillApplySubmit(query.reqHeader);
        RetAtsB101BillApplySubmitDetail detail=new RetAtsB101BillApplySubmitDetail();
        retAts.writeBeforeDetail();
        //***************1、新增票据信息***************
        //赋值通用参数
        CMBC01Header bankHeader = new CMBC01Header();
        bankHeader.trnCode = "IssueDraftAdd";
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        while(query.readRecord()){
            xmlWriter.writeStartElement("xDataBody");
            xmlWriter.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
            xmlWriter.writeElementString("insId", getQryTransID());//客户端产生的交易流水号（唯一)
            xmlWriter.writeElementString("appAcNo", query.detail.drawAct);//签约账号（★） 32
            xmlWriter.writeElementString("billType", query.detail.billType);// 票据类型:1-银票 2-商票（★） 1
            xmlWriter.writeElementString("billClass", "2");// 票据种类:1-实物(暂不支持) 2-电子（★） 1
            String issDate=query.detail.issDate.substring(0,10);
            String dueDate=query.detail.dueDate.substring(0,10);
            xmlWriter.writeElementString("issueDt", issDate);// 出票日(yyyy-MM-dd)（★）
            xmlWriter.writeElementString("dueDt", dueDate);//票面到期日(yyyy-MM-dd)（★）
            xmlWriter.writeElementString("billMoney", query.detail.billAmt);// 票面金额（★） 12
            xmlWriter.writeElementString("remitter", query.detail.drawName);// 出票人名称（★） 300
            xmlWriter.writeElementString("remitterAcct", query.detail.drawAct);// 出票人账号（★） 32
            xmlWriter.writeElementString("remitterBank", query.detail.drawBnkName);// 出票人开户行（★） 128
            xmlWriter.writeElementString("remitterBankNo", query.detail.drawBnkCode);//出票人开户行行号（★） 12
            xmlWriter.writeElementString("payee", query.detail.payeeName);// 票面收款人名称（★） 60
            xmlWriter.writeElementString("payeeAcct", query.detail.payeeAct);// 票面收款人账号（★） 50
            xmlWriter.writeElementString("payeeBank", query.detail.payeeBnkName);// 票面收款人开户行名称（★） 128
            xmlWriter.writeElementString("payeeBankNo", query.detail.payeeBnkCode);// 票面收款人开户行行号（★） 12
            xmlWriter.writeElementString("acceptor", query.detail.accpName);// 承兑人名称（★） 36
            xmlWriter.writeElementString("acceptorAcct", query.detail.accpAct);// 承兑人账号（★） 32
            xmlWriter.writeElementString("acceptorBank", query.detail.accpBnkName);// 承兑人开户行名称 128
            xmlWriter.writeElementString("acceptorBankNo", query.detail.accpBnkCode);// 承兑人开户行行号（★） 12
            xmlWriter.writeElementString("banEndorsementMark", "1".equals(query.detail.transFlag)?"0":"1");// 不得转让标记（★） (0-可转让； 1-不得转让) 1
            xmlWriter.writeElementString("conferNo", query.detail.contractNo);// 合同文本编号
            xmlWriter.writeElementString("invoiceNo", query.detail.invoiceNo);//发票号
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if(tmpBkReader != null){
                    String tmpRetCode3 = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                    String tmpRetMessage3 = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                    if ("0".equals(tmpRetCode3)) {
                        //***************2、查询可登记票据信息列表***************
                        CMBC01Header bankHeader2 = new CMBC01Header();
                        bankHeader2.trnCode = "QryStayRegDraft";
                        XmlTextWriter xmlWriter2 = new XmlTextWriter();//发银行报文的XML对象
                        //写入通用的头部节点
                        writeCommonHeader(xmlWriter2, bankHeader2);
                        xmlWriter2.writeStartElement("xDataBody");
                        xmlWriter2.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
                        xmlWriter2.writeElementString("appAcNo", query.detail.drawAct);//签约账号（★） 32
                        xmlWriter2.writeElementString("currentIndex", "1");
                        xmlWriter2.writeElementString("pageSize", "100");
                        xmlWriter2.writeEndElement();
                        xmlWriter2.writeEndDocument();
                        String[] tmpStrRet2 = sendToBank(xmlWriter2.toXmlString());//发送银行
                        if (tmpStrRet2[0].length() == 0) {
                            StringBuilder tmpSbMsg2 = new StringBuilder();
                            XmlTextReader tmpBkReader2 = loadBankRespXmlReader(tmpStrRet2[1], tmpSbMsg2);
                            if(tmpBkReader2 != null){
                                String rgctId="";//票据id
                                while(tmpBkReader2.read("/CMBC/xDataBody/draft/List/Map")){
                                    boolean issDateFlag=tmpBkReader2.readedXElement.getSingleTextNotNull("issueDt").equals(issDate);//出票日
                                    boolean dueDateFlag=tmpBkReader2.readedXElement.getSingleTextNotNull("dueDt").equals(dueDate);//到期日
                                    boolean billTypeFlag=tmpBkReader2.readedXElement.getSingleTextNotNull("billType").equals(query.detail.billType);//票据类型
                                    boolean billMoneyFlag=(new BigDecimal(tmpBkReader2.readedXElement.getSingleTextNotNull("billMoney")).subtract(new BigDecimal(query.detail.billAmt))).intValue()==0;//票据金额
                                    boolean transFlag=tmpBkReader2.readedXElement.getSingleTextNotNull("banEndorsementMark").equals("1".equals(query.detail.transFlag)?"0":"1");//不得转让标志
                                    boolean remitterAcctFlag=tmpBkReader2.readedXElement.getSingleTextNotNull("remitterAcct").equals(query.detail.drawAct);//出票人
                                    boolean payeeAcctFlag=tmpBkReader2.readedXElement.getSingleTextNotNull("payeeAcct").equals(query.detail.payeeAct);//收票人
                                    boolean accepFlag=tmpBkReader2.readedXElement.getSingleTextNotNull("acceptor").equals(query.detail.accpName);//承兑人
                                    if(issDateFlag && dueDateFlag && billTypeFlag && billMoneyFlag && remitterAcctFlag && payeeAcctFlag && transFlag && accepFlag){
                                        rgctId=tmpBkReader2.readedXElement.getSingleTextNotNull("rgctId");
                                    }else {
                                        continue;
                                    }
                                }
                                if(StringHelper.hasAnyChar(rgctId)){
                                    //***************3、出票登记***************
                                    CMBC01Header bankHeader3 = new CMBC01Header();
                                    bankHeader3.trnCode = "DraftReg";
                                    XmlTextWriter xmlWriter3 = new XmlTextWriter();//发银行报文的XML对象
                                    //写入通用的头部节点
                                    writeCommonHeader(xmlWriter3, bankHeader3);
                                    xmlWriter3.writeStartElement("xDataBody");
                                    xmlWriter3.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
                                    xmlWriter3.writeElementString("insId", query.detail.reqSeqID);//客户端产生的交易流水号（唯一)
                                    xmlWriter3.writeElementString("appAcNo", query.detail.drawAct);//签约账号（★） 32
                                    xmlWriter3.writeElementString("rgctId", rgctId);//票据id
                                    xmlWriter3.writeEndElement();
                                    xmlWriter3.writeEndDocument();
                                    String[] tmpStrRet3 = sendToBank(xmlWriter3.toXmlString());//发送银行
                                    if (tmpStrRet3[0].length() == 0) {
                                        StringBuilder tmpSbMsg3 = new StringBuilder();
                                        XmlTextReader tmpBkReader3 = loadBankRespXmlReader(tmpStrRet3[1], tmpSbMsg3);
                                        if(tmpBkReader3 != null){
                                            String tmpRetCode = tmpBkReader3.getFirstText("/CMBC/responseHeader/status/code");
                                            String tmpRetMessage = tmpBkReader3.getFirstText("/CMBC/responseHeader/status/message");
                                            detail.respCode = "0";
                                            detail.respInfo = tmpRetMessage;
                                            detail.reqSeqID = query.detail.reqSeqID;
                                            detail.payInfoCode = tmpRetCode;
                                            detail.transState = detail.payInfoCode;
                                        }else {
                                            detail.respCode = "-1";
                                            detail.respInfo = tmpSbMsg3.toString();
                                        }
                                    }else {
                                        detail.respCode = "-1";
                                        detail.respInfo = tmpStrRet3[1];
                                    }
                                }else {
                                    detail.respCode = "-1";
                                    detail.respInfo ="查无可登记票据信息，出票失败";
                                }
                            }else {
                                detail.respCode = "-1";
                                detail.respInfo = tmpSbMsg2.toString();
                            }
                        }else {
                            detail.respCode = "-1";
                            detail.respInfo = tmpStrRet2[1];
                        }
                    }else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpRetMessage3;
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = tmpStrRet[1];
            }
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 提示承兑 b2e0105 ok
     */
    @Override
    public RetAtsB102BillAccpSubmit D_B102_BillResp_Submit(ReqB102BillAccpSubmit query) throws Exception{
        RetAtsB102BillAccpSubmit retAts=new RetAtsB102BillAccpSubmit(query.reqHeader);
        RetAtsB102BillAccpSubmitDetail detail=new RetAtsB102BillAccpSubmitDetail();
        retAts.writeBeforeDetail();
        //赋值通用参数
        CMBC01Header bankHeader = new CMBC01Header();
        bankHeader.trnCode = "AcceptDraft";
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        Connection conn=FGBiz.getLocalDbConn();
        while (query.readRecord()) {
            try{
                xmlWriter.writeStartElement("xDataBody");
                xmlWriter.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
                xmlWriter.writeElementString("insId", query.detail.reqSeqID);//客户端产生的交易流水号（唯一)
                //根据票据号获取关联的票据id
                String billId="";
                Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (selMap.size() > 0) {
                    billId=selMap.get("BANKREQSEQID").toString();
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = "票据号与票据id关联映射异常";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("rgctId", billId);//票据Id
                xmlWriter.writeElementString("appAcNo", query.detail.actNum);//客户账号
                xmlWriter.writeElementString("invoiceNo", "");//发票号，出票时填写的信息
                xmlWriter.writeElementString("batchNo", "");//每次申请都是同一个批次，会有一个批次号(若输入时，长度不能小于15)
                xmlWriter.writeElementString("remark", "");//备注
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if(tmpBkReader != null){
                        String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                        String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                        detail.respCode = "0";
                        detail.respInfo = tmpRetMessage;
                        detail.reqSeqID = query.detail.reqSeqID;
                        detail.payInfoCode = tmpRetCode;
                        detail.transState = detail.payInfoCode;
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1].toString();
                }
            } catch (Exception e) {
                e.printStackTrace();
                WriteBankLogLn("撤票异常：" + e.getMessage());
            } finally {
                conn.close();
            }
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 撤票 b2e0104 ok

     */
    @Override
    public RetAtsB103BillRetSubmit D_B103_BillResp_Submit(ReqB103BillRetSubmit query) throws Exception{
        RetAtsB103BillRetSubmit retAts = new RetAtsB103BillRetSubmit(query.reqHeader);
        RetAtsB103BillRetSubmitDetail detail=new RetAtsB103BillRetSubmitDetail();
        retAts.writeBeforeDetail();
        //赋值通用参数
        CMBC01Header bankHeader = new CMBC01Header();
        bankHeader.trnCode = "IssueDestruction";
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        Connection conn=FGBiz.getLocalDbConn();
        while (query.readRecord()) {
            try{
                xmlWriter.writeStartElement("xDataBody");
                xmlWriter.writeElementString("trnId",getQryTransID());//客户端产生的交易唯一标志
                xmlWriter.writeElementString("insId", query.detail.reqSeqID);//客户端产生的交易流水号（唯一)
                xmlWriter.writeElementString("appAcNo", query.detail.actNum);//客户账号
                //根据票据号获取关联的票据id
                String billId="";
                Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (selMap.size() > 0) {
                    billId=selMap.get("BANKREQSEQID").toString();
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = "票据号与票据id关联映射异常";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("rgctId", billId);//票据Id
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if(tmpBkReader != null){
                        String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                        String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                        detail.respCode = "0";
                        detail.respInfo = tmpRetMessage;
                        detail.reqSeqID = query.detail.reqSeqID;
                        detail.payInfoCode = tmpRetCode;
                        detail.transState = detail.payInfoCode;
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1].toString();
                }
            } catch (Exception e) {
                e.printStackTrace();
                WriteBankLogLn("撤票异常：" + e.getMessage());
            } finally {
                conn.close();
            }
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 提示收票 b2e0108

     */
    @Override
    public RetAtsB104BillReceSubmit D_B104_BillResp_Submit(ReqB104BillReceSubmit query) throws Exception{
        RetAtsB104BillReceSubmit retAts=new RetAtsB104BillReceSubmit(query.reqHeader);
        RetAtsB104BillReceSubmitDetail detail=new RetAtsB104BillReceSubmitDetail();
        retAts.writeBeforeDetail();
        Connection conn=FGBiz.getLocalDbConn();
        while(query.readRecord()){
            try{
                //赋值通用参数
                CMBC01Header bankHeader = new CMBC01Header();
                bankHeader.trnCode = "Issuance";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("xDataBody");
                xmlWriter.writeElementString("insId", query.detail.reqSeqID);//客户端产生的交易流水号（唯一)
                xmlWriter.writeElementString("appAcNo", query.detail.actNum);//客户账号
                //根据票据号获取关联的票据id
                String billId="";
                Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (selMap.size() > 0) {
                    billId=selMap.get("BANKREQSEQID").toString();
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = "票据号与票据id关联映射异常";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("rgctId", billId);//票据Id
                xmlWriter.writeElementString("remark", query.detail.memo);//备注
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if(tmpBkReader != null){
                        String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                        String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                        detail.respCode = "0";
                        detail.respInfo = tmpRetMessage;
                        detail.reqSeqID = query.detail.reqSeqID;
                        detail.payInfoCode = tmpRetCode;
                        detail.transState = detail.payInfoCode;
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1].toString();
                }
            } catch (Exception e) {
                e.printStackTrace();
                WriteBankLogLn("撤票异常：" + e.getMessage());
            } finally {
                conn.close();
            }
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 背书转让申请 b2e0280  ok

     */
    @Override
    public RetAtsB105BillEndoSubmit D_B105_BillResp_Submit(ReqB105BillEndoSubmit query) throws Exception{
        RetAtsB105BillEndoSubmit retAts=new RetAtsB105BillEndoSubmit(query.reqHeader);
        RetAtsB105BillEndoSubmitDetail detail=new RetAtsB105BillEndoSubmitDetail();
        retAts.writeBeforeDetail();
        Connection conn=FGBiz.getLocalDbConn();
        while(query.readRecord()) {
            try {
                //赋值通用参数
                CMBC01Header bankHeader = new CMBC01Header();
                bankHeader.trnCode = "EndorsmtApply";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("xDataBody");
                xmlWriter.writeElementString("insId", query.detail.reqSeqID);//客户端产生的交易流水号（唯一)
                xmlWriter.writeElementString("appAcNo", query.detail.actNum);//客户账号
                //根据票据号获取关联的票据id
                String billId="";
                Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (selMap.size() > 0) {
                    billId=selMap.get("BANKREQSEQID").toString();
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = "票据号与票据id关联映射异常";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("rgctId", billId);//票据Id
                xmlWriter.writeElementString("receiverName", query.detail.receName);//接收人客户名称
                xmlWriter.writeElementString("receiverAcctNo", query.detail.receAct);//接收人账号
                xmlWriter.writeElementString("receiverBankNo", query.detail.receBnkCode);//接收人开户行行号
                String transFlag=query.detail.transFlag;
                if("1".equals(transFlag)){
                    transFlag="0";
                }else{
                    transFlag="1";
                }
                xmlWriter.writeElementString("banEndorsementMark", transFlag);//不得转让标记（★）(0-可转让；1-不得转让)
                xmlWriter.writeElementString("remark", query.detail.memo);//备注
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                        String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                        detail.respCode = "0";
                        detail.respInfo = tmpRetMessage;
                        detail.reqSeqID = query.detail.reqSeqID;
                        detail.payInfoCode = tmpRetCode;
                        detail.transState = detail.payInfoCode;
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1];
                }
            } catch (Exception e) {
                e.printStackTrace();
                WriteBankLogLn("背书异常：" + e.getMessage());
            } finally {
                conn.close();
            }
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 贴现申请 b2e0110 ok

     */
    @Override
    public RetAtsB106BillDiscSubmit D_B106_BillResp_Submit(ReqB106BillDiscSubmit query) throws Exception{
        RetAtsB106BillDiscSubmit retAts=new RetAtsB106BillDiscSubmit(query.reqHeader);
        RetAtsB106BillDiscSubmitDetail detail=new RetAtsB106BillDiscSubmitDetail();
        retAts.writeBeforeDetail();
        Connection conn=FGBiz.getLocalDbConn();
        while(query.readRecord()) {
            try {
                //赋值通用参数
                CMBC01Header bankHeader = new CMBC01Header();
                bankHeader.trnCode = "DiscountableBillApply";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("xDataBody");
                xmlWriter.writeElementString("insId", query.detail.reqSeqID);//客户端产生的交易流水号（唯一)
                //根据票据号获取关联的票据id
                String billId="";
                Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (selMap.size() > 0) {
                    billId=selMap.get("BANKREQSEQID").toString();
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = "票据号与票据id关联映射异常";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("rgctId", billId);//票据Id
                xmlWriter.writeElementString("appAcNo", query.detail.actNum);//客户账号
                String discType=query.detail.discType;
                if("1".equals(discType)){
                    discType="0";
                }else if("2".equals(discType)){
                    discType="1";
                }
                xmlWriter.writeElementString("discType", discType);//贴现方式： 0－买断式 1－赎回式(暂不支持)
                xmlWriter.writeElementString("discRate", query.detail.discRate);//贴现利率
                String disDate=query.detail.discDate;
                if(StringHelper.isNullOrEmpty(disDate)){
                    disDate=Format.Date10Format.format(new Date());
                }
                xmlWriter.writeElementString("disDate", disDate);//贴现日期
                xmlWriter.writeElementString("rpdOpenDt", query.detail.repOpenDate);//赎回开放日(格式： YYYY-MM-dd)
                xmlWriter.writeElementString("rpdDueDt", query.detail.repDueDate);//赎回截止日(格式： YYYY-MM-dd)
                xmlWriter.writeElementString("rpdDiscRate", query.detail.repRate);//赎回利率
                xmlWriter.writeElementString("rpdDiscAmt", query.detail.repAmt);//赎回金额
                xmlWriter.writeElementString("settlementMark", getBankConfig().getMapValueByKey("payTypeMapping",query.detail.payType));//线上清算： 0.线下， 1.线上
                xmlWriter.writeElementString("inAcctNo", query.detail.actedAct);//入账帐号
                xmlWriter.writeElementString("inBankNo", query.detail.actedBnkCode);//入账行号
                String transFlag=query.detail.transFlag;
                if("1".equals(transFlag)){
                    transFlag="0";
                }else{
                    transFlag="1";
                }
                xmlWriter.writeElementString("banEndorsementMark", transFlag);//不得转让标记： 0-可转让 1-不可转让
                xmlWriter.writeElementString("receiverAcctNo", query.detail.receAct);//贴入人账号
                xmlWriter.writeElementString("receiverBankNo", query.detail.receBnkCode);//贴入人行号
                xmlWriter.writeElementString("receiverName", query.detail.receBnkName);//贴入人行名
                xmlWriter.writeElementString("batchNo", "");//批次号
                xmlWriter.writeElementString("conferNo", query.detail.contractNo);//合同文本编号
                xmlWriter.writeElementString("invoiceNo", query.detail.invoiceNo);//发票号
                xmlWriter.writeElementString("remark", query.detail.memo);//贴出人备注
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                        String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                        detail.respCode = "0";
                        detail.respInfo = tmpRetMessage;
                        detail.reqSeqID = query.detail.reqSeqID;
                        detail.payInfoCode = tmpRetCode;
                        detail.transState = detail.payInfoCode;
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1];
                }
            } catch (Exception e) {
                e.printStackTrace();
                WriteBankLogLn("贴现异常：" + e.getMessage());
            } finally {
                conn.close();
            }
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 质押申请 b2e0285
     暂无接口
     */
    @Override
    public RetAtsB107BillPledSubmit D_B107_BillResp_Submit(ReqB107BillPledSubmit query) throws Exception{
        return null;
    }

    /**
     * 提示付款 b2e0111  ok

     */
    @Override
    public RetAtsB108BillPaySubmit D_B108_BillResp_Submit(ReqB108BillPaySubmit query) throws Exception{
        RetAtsB108BillPaySubmit retAts = new RetAtsB108BillPaySubmit(query.reqHeader);
        RetAtsB108BillPaySubmitDetail detail=new RetAtsB108BillPaySubmitDetail();
        retAts.writeBeforeDetail();
        //赋值通用参数
        CMBC01Header bankHeader = new CMBC01Header();
        bankHeader.trnCode = "IssueDraft";
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        Connection conn=FGBiz.getLocalDbConn();
        while (query.readRecord()) {
            try {
                xmlWriter.writeStartElement("xDataBody");
                xmlWriter.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
                xmlWriter.writeElementString("insId", query.detail.reqSeqID);//客户端产生的交易流水号（唯一)
                xmlWriter.writeElementString("appAcNo", query.detail.actNum);//客户账号
                //根据票据号获取关联的票据id
                String billId="";
                Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (selMap.size() > 0) {
                    billId=selMap.get("BANKREQSEQID").toString();
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = "票据号与票据id关联映射异常";
                    retAts.writeDetail(detail);
                    retAts.writeEnd();
                    return retAts;
                }
                xmlWriter.writeElementString("rgctId", billId);//票据Id
                xmlWriter.writeElementString("settlementMark", getBankConfig().getMapValueByKey("payTypeMapping",query.detail.payType));//线上清算标记(0-线下清算；1-线上清算)
                xmlWriter.writeElementString("overdueMark", getBankConfig().getMapValueByKey("proPayTypeMapping",query.detail.proPayType));//逾期标记(0-非逾期；1-逾期)
                xmlWriter.writeElementString("overdueReason", query.detail.overDueRea);//逾期原因(逾期标记为1时，必填)
                xmlWriter.writeElementString("remark",query.detail.memo);//备注
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if(tmpBkReader != null){
                        String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                        String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                        detail.respCode = "0";
                        detail.respInfo = tmpRetMessage;
                        detail.reqSeqID = query.detail.reqSeqID;
                        detail.payInfoCode = tmpRetCode;
                        detail.transState = detail.payInfoCode;
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1].toString();
                }
            } catch (Exception e) {
                e.printStackTrace();
                WriteBankLogLn("提示付款异常：" + e.getMessage());
            } finally {
                conn.close();
            }
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 保证申请  b2e0281
     暂无接口
     */
    @Override
    public RetAtsB109BillGuarSubmit D_B109_BillResp_Submit(ReqB109BillGuarSubmit query) throws Exception{
        return null;
    }

    /**
     * 追索通知 b2e0282
     暂无接口
     */
    @Override
    public RetAtsB110BillRecoSubmit D_B110_BillResp_Submit(ReqB110BillRecoSubmit query) throws Exception{
        return null;
    }

    /**
     * 同意清偿 b2e0112
     暂无接口
     */
    @Override
    public RetAtsB111BillAgreeSubmit D_B111_BillResp_Submit(ReqB111BillAgreeSubmit query) throws Exception{
        return null;
    }

    /**
     * 交易撤回 b2e0106  ok
     */
    @Override
    public RetAtsB112BillCancSubmit D_B112_BillResp_Submit(ReqB112BillCancSubmit query) throws Exception{
        RetAtsB112BillCancSubmit retAts=new RetAtsB112BillCancSubmit(query.reqHeader);
        RetAtsB112BillCancSubmitDetail detail=new RetAtsB112BillCancSubmitDetail();
        retAts.writeBeforeDetail();
        try{
            while (query.readRecord()) {
                if ("1".equals(query.detail.cancelType)) {
                    detail=b112BillRespSubmitOne(query,"CancelAcceptDraft");//1-承兑申请撤回ok
                }else if ("6".equals(query.detail.cancelType)){
                    detail=b112BillRespSubmitOne(query,"CancelIssueDraft");//6-（逾期）提示付款撤回ok
                }else if ("2".equals(query.detail.cancelType)){
                    detail=b112BillRespSubmitOne(query,"CancelIssuanceDraft");//2-提示收票撤销ok
                }else if ("3".equals(query.detail.cancelType)){
                    detail=b112BillRespSubmitOne(query,"CancelEndorsmt");//3-背书撤回ok
                }else if ("4".equals(query.detail.cancelType)){
                    detail=b112BillRespSubmitOne(query,"CancelDiscountBill");//4-贴现撤回ok
                }else {
                    detail.respCode="-1";
                    detail.respInfo="暂不支持此撤回操作";
                }
            }
        }catch (Exception ex ){
            detail.respCode="-1";
            detail.respInfo="查询异常";
            ex.printStackTrace();
            WriteBankLogLn("查询异常:"+ex);
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB113ProtocolSubmit D_B113_ProtocolResp_Submit(ReqB113ProtocolSubmit query) throws Exception {
        return null;
    }

    /**
     * 新增票据信息
     * @param query
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsB114BillAddSubmit D_B114_BillAddSubmit(ReqB114BillAddSubmit query) throws Exception {
        RetAtsB114BillAddSubmit retAts=new RetAtsB114BillAddSubmit(query.reqHeader);
        RetAtsB114BillAddSubmitDetail detail=new RetAtsB114BillAddSubmitDetail();
        retAts.writeBeforeDetail();
        //赋值通用参数
        CMBC01Header bankHeader = new CMBC01Header();
        bankHeader.trnCode = "IssueDraftAdd";
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        while(query.readRecord()){
            xmlWriter.writeStartElement("xDataBody");
            xmlWriter.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
            xmlWriter.writeElementString("insId", query.detail.reqSeqID);//客户端产生的交易流水号（唯一)
            xmlWriter.writeElementString("appAcNo", query.detail.actNum);//签约账号（★） 32
            xmlWriter.writeElementString("billType", query.detail.billType);// 票据类型:1-银票 2-商票（★） 1
            xmlWriter.writeElementString("billClass", query.detail.billClass);// 票据种类:1-实物(暂不支持) 2-电子（★） 1
            String issDate=query.detail.issDate.substring(0,10);
            String dueDate=query.detail.dueDate.substring(0,10);
            xmlWriter.writeElementString("issueDt", issDate);// 出票日(yyyy-MM-dd)（★）
            xmlWriter.writeElementString("dueDt", dueDate);//票面到期日(yyyy-MM-dd)（★）
            xmlWriter.writeElementString("billMoney", query.detail.billAmt);// 票面金额（★） 12
            xmlWriter.writeElementString("remitter", query.detail.drawName);// 出票人名称（★） 300
            xmlWriter.writeElementString("remitterAcct", query.detail.drawAct);// 出票人账号（★） 32
            xmlWriter.writeElementString("remitterBank", query.detail.drawBnkName);// 出票人开户行（★） 128
            xmlWriter.writeElementString("remitterBankNo", query.detail.drawBnkCode);//出票人开户行行号（★） 12
            xmlWriter.writeElementString("payee", query.detail.payeeName);// 票面收款人名称（★） 60
            xmlWriter.writeElementString("payeeAcct", query.detail.payeeAct);// 票面收款人账号（★） 50
            xmlWriter.writeElementString("payeeBank", query.detail.payeeBnkName);// 票面收款人开户行名称（★） 128
            xmlWriter.writeElementString("payeeBankNo", query.detail.payeeBnkCode);// 票面收款人开户行行号（★） 12
            xmlWriter.writeElementString("acceptor", query.detail.accpName);// 承兑人名称（★） 36
            xmlWriter.writeElementString("acceptorAcct", query.detail.accpAct);// 承兑人账号（★） 32
            xmlWriter.writeElementString("acceptorBank", query.detail.accpBnkName);// 承兑人开户行名称 128
            xmlWriter.writeElementString("acceptorBankNo", query.detail.accpBnkCode);// 承兑人开户行行号（★） 12
            xmlWriter.writeElementString("banEndorsementMark", "1".equals(query.detail.transFlag)?"0":"1");// 不得转让标记（★） (0-可转让； 1-不得转让) 1
            xmlWriter.writeElementString("conferNo", query.detail.contractNo);// 合同文本编号
            xmlWriter.writeElementString("invoiceNo", query.detail.invoiceNo);//发票号
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if(tmpBkReader != null){
                    String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                    String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                    detail.respCode = "0";
                    detail.respInfo = tmpRetMessage;
                    detail.reqSeqID = tmpBkReader.getFirstTextNotNull("/CMBC/xDataBody/insId");
                    detail.payInfoCode = tmpRetCode;
                    detail.transState = detail.payInfoCode;
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = tmpStrRet[1];
            }
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 删除票据信息
     * @param query
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsB115BillDeleteSubmit D_B115_BillDeleteSubmit(ReqB115BillDeleteSubmit query) throws Exception {
        RetAtsB115BillDeleteSubmit retAts=new RetAtsB115BillDeleteSubmit(query.reqHeader);
        RetAtsB115BillDeleteSubmitDetail detail=new RetAtsB115BillDeleteSubmitDetail();
        retAts.writeBeforeDetail();
        Connection conn=FGBiz.getLocalDbConn();
        while(query.readRecord()) {
            try {
                //赋值通用参数
                CMBC01Header bankHeader = new CMBC01Header();
                bankHeader.trnCode = "IssueDraftDel";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("xDataBody");
                xmlWriter.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
                xmlWriter.writeElementString("insId", query.detail.reqSeqID);//客户端产生的交易流水号（唯一)
                xmlWriter.writeElementString("appAcNo", query.detail.actNum);//签约账号（★） 32
                //根据票据号获取关联的票据id
                String billId="";
                Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (selMap.size() > 0) {
                    billId=selMap.get("BANKREQSEQID").toString();
                } else {
                    billId=query.detail.billNo;
                }
                xmlWriter.writeElementString("rgctId", billId);//票据Id
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                        String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                        detail.respCode = "0";
                        detail.respInfo = tmpRetMessage;
                        detail.reqSeqID = tmpBkReader.getFirstTextNotNull("/CMBC/xDataBody/insId");
                        detail.payInfoCode = tmpRetCode;
                        detail.transState = detail.payInfoCode;
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1];
                }
            } catch (Exception e) {
                e.printStackTrace();
                WriteBankLogLn("贴现异常：" + e.getMessage());
            } finally {
                conn.close();
            }
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 出票登记
     * @param query
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsB116BillApplySubmit D_B116_BillApplySubmit(ReqB116BillApplySubmit query) throws Exception {
        RetAtsB116BillApplySubmit retAts=new RetAtsB116BillApplySubmit(query.reqHeader);
        RetAtsB116BillApplySubmitDetail detail=new RetAtsB116BillApplySubmitDetail();
        retAts.writeBeforeDetail();
        Connection conn=FGBiz.getLocalDbConn();
        while(query.readRecord()) {
            try {
                //赋值通用参数
                CMBC01Header bankHeader = new CMBC01Header();
                bankHeader.trnCode = "DraftReg";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("xDataBody");
                xmlWriter.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
                xmlWriter.writeElementString("insId", query.detail.reqSeqID);//客户端产生的交易流水号（唯一)
                xmlWriter.writeElementString("appAcNo", query.detail.actNum);//签约账号（★） 32
                //根据票据号获取关联的票据id
                String billId="";
                Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
                if (selMap.size() > 0) {
                    billId=selMap.get("BANKREQSEQID").toString();
                } else {
                    billId=query.detail.billNo;
                }
                xmlWriter.writeElementString("rgctId", billId);//票据Id
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                        String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                        detail.respCode = "0";
                        detail.respInfo = tmpRetMessage;
                        detail.reqSeqID = tmpBkReader.getFirstTextNotNull("/CMBC/xDataBody/insId");
                        detail.payInfoCode = tmpRetCode;
                        detail.transState = detail.payInfoCode;
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[1];
                }
            } catch (Exception e) {
                e.printStackTrace();
                WriteBankLogLn("贴现异常：" + e.getMessage());
            }finally {
                conn.close();
            }
        }
        retAts.writeDetail(detail);
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB119BillRelePledSubmit D_B119_BillResp_Submit(ReqB119BillRelePledSubmit query) throws Exception {
        return null;
    }

    /**
     * 通用应答
     * @param query
     * @return
     * @throws Exception
     */
    private RetAtsB003BillRespSubmitDetail b003BillRespSubmitOne(ReqB003BillRespSubmit query,String trnCode) throws Exception {
        RetAtsB003BillRespSubmitDetail detail = new RetAtsB003BillRespSubmitDetail();
        //赋值通用参数
        CMBC01Header bankHeader = new CMBC01Header();
        bankHeader.trnCode = trnCode;
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        Connection conn=FGBiz.getLocalDbConn();
        try{
            xmlWriter.writeStartElement("xDataBody");
            if("AcceptSignUp".equals(trnCode) || "IssueSignUp".equals(trnCode)){
                xmlWriter.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
            }
            xmlWriter.writeElementString("insId", query.detail.reqSeqID);//客户端产生的交易流水号（唯一)
            //根据票据号获取关联的票据id
            String billId="";
            Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
            if (selMap.size() > 0) {
                billId=selMap.get("BANKREQSEQID").toString();
            } else {
                detail.respCode = "-1";
                detail.respInfo = "票据号与票据id关联映射异常";
                return detail;
            }
            xmlWriter.writeElementString("rgctId", billId);//票据Id
            xmlWriter.writeElementString("appAcNo", query.detail.actNum);//客户账号
            xmlWriter.writeElementString("signUpMark", "2".equals(query.detail.respFlag)?"0":query.detail.respFlag);//签收标志，0-拒绝 1-同意
            xmlWriter.writeElementString("remark", query.detail.memo);//备注
            if("IssueSignUp".equals(trnCode)){
                if (query.detail.respFlag.equals("2")) {
                    if (query.detail.unPayCode.equals("99")) {
                        xmlWriter.writeElementString("payRefuseCode","DC09");//拒绝代码
                    } else if (Integer.parseInt(query.detail.unPayCode) > 0 && Integer.parseInt(query.detail.unPayCode) <= 9) {
                        int unPayCode = Integer.parseInt(query.detail.unPayCode);
                        xmlWriter.writeElementString("payRefuseCode","DC0" + (unPayCode - 1));//拒绝代码
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = "拒付理由代码错误";
                        WriteBankLogLn("拒付理由代码错误");
                        return detail;
                    }
                }else {
                    xmlWriter.writeElementString("payRefuseCode", "");//拒付代码（签收标识 0 时输入）
                }
                xmlWriter.writeElementString("rejectReason", query.detail.unPayMemo);//拒付理由（签收标识 0 时输入）
            }
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if(tmpBkReader != null){
                    String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                    String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                    detail.respCode = "0";
                    detail.respInfo = tmpRetMessage;
                    detail.reqSeqID = tmpBkReader.getFirstText("/CMBC/xDataBody/insId");
                    detail.payInfoCode = tmpRetCode;
                    detail.transState = detail.payInfoCode;
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = tmpStrRet[1].toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
            WriteBankLogLn("应答异常：" + e.getMessage());
        } finally {
            conn.close();
        }
        return detail;
    }

    /**
     * 通用撤回
     * @param query
     * @return
     * @throws Exception
     */
    private RetAtsB112BillCancSubmitDetail b112BillRespSubmitOne(ReqB112BillCancSubmit query,String trnCode) throws Exception {
        RetAtsB112BillCancSubmitDetail detail = new RetAtsB112BillCancSubmitDetail();
        //赋值通用参数
        CMBC01Header bankHeader = new CMBC01Header();
        bankHeader.trnCode = trnCode;
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        Connection conn=FGBiz.getLocalDbConn();
        try{
            xmlWriter.writeStartElement("xDataBody");
            if("CancelAcceptDraft".equals(trnCode) || "CancelIssueDraft".equals(trnCode)){
                xmlWriter.writeElementString("trnId", getQryTransID());//客户端产生的交易唯一标志
            }
            xmlWriter.writeElementString("insId", query.detail.reqSeqID);//客户端产生的交易流水号（唯一)
            //根据票据号获取关联的票据id
            String billId="";
            Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQ, new Object[]{query.detail.billNo, query.reqHeader.bankCode});
            if (selMap.size() > 0) {
                billId=selMap.get("BANKREQSEQID").toString();
            } else {
                detail.respCode = "-1";
                detail.respInfo = "票据号与票据id关联映射异常";
                return detail;
            }
            xmlWriter.writeElementString("rgctId", billId);//票据Id
            xmlWriter.writeElementString("appAcNo", query.detail.actNum);//客户账号
            if("CancelIssueDraft".equals(trnCode)){
                xmlWriter.writeElementString("reason", query.detail.memo);//撤销原因
                xmlWriter.writeElementString("remark", query.detail.memo);//备注
            }
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if(tmpBkReader != null){
                    String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                    String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                    detail.respCode = "0";
                    detail.respInfo = tmpRetMessage;
                    detail.reqSeqID = query.detail.reqSeqID;
                    detail.payInfoCode = tmpRetCode;
                    detail.transState = detail.payInfoCode;
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = tmpStrRet[1].toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
            WriteBankLogLn("撤回异常：" + e.getMessage());
        } finally {
            conn.close();
        }
        return detail;
    }
}
